﻿using CIS.Web.Common;
using CIS.Web.Models;
using CIS.Web.Properties;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Linq.Dynamic;
using System.Threading.Tasks;
using System.Web.Mvc;
using static System.Net.Mime.MediaTypeNames;

namespace CIS.Web.Controllers
{
	[RoutePrefix("ConferenceScheduleReport")]
	public class ConferenceScheduleReportController : BaseDbController
	{
		[Route("Index/{ConferenceId}")]
		public async Task<ActionResult> Index(string conferenceId)
		{
			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName, "Index", conferenceId);

			var conferenceMaster = await DbContext.ConferenceMaster.AsNoTracking()
					.FirstOrDefaultAsync(x => x.Status == ConstValues.Status.正常 &&
						x.ID == conferenceId);

			ViewBag.ConferenceId = conferenceId;
			ViewBag.ConferenceName = conferenceMaster.ConferenceName;
			ViewBag.ControllerName = Resources.ConferenceScheduleReport;
			ViewBag.Language = conferenceMaster.IsInternational ? ConstValues.Language.En : ConstValues.Language.Cn;

			var model = new ConferenceScheduleReportModel();
			model.ConferenceId = conferenceId;
			LoginUser.ConferenceFunctionIdList = Utility.GetConferenceFunctionIdList(conferenceId);
			return View(model);
		}

		public async Task<JsonCamelCaseResult> GetData(int? page, int? rows, string sort, string order, ConferenceScheduleReportModel model)
		{
			int take = rows ?? ConstValues.DefaultGridRows;
			int skip = ((page ?? 1) - 1) * take;

			var sql = from contributeInfo in DbContext.ConferenceContributeInfo.AsNoTracking()
						.Where(x => x.ConferenceId == model.ConferenceId &&
						(!string.IsNullOrEmpty(x.AdminStatus) ? x.AdminStatus == ConstValues.ContributationStatus.录用 :
							x.ContributationStatus == ConstValues.ContributationStatus.录用))
				  join conferenceMaster in DbContext.ConferenceMaster.AsNoTracking().Where(x => x.Status == ConstValues.Status.正常)
					  on contributeInfo.ConferenceId equals conferenceMaster.ID
				  join reportMaster in DbContext.ReportMaster.AsNoTracking().Where(x => x.Status == ConstValues.Status.正常)
					  on contributeInfo.ReportId equals reportMaster.ID into t1
				  from report in t1.DefaultIfEmpty()
				  join topicMaster in DbContext.TopicMaster.AsNoTracking().Where(x => x.Status == ConstValues.Status.正常)
					  on contributeInfo.TopicId equals topicMaster.ID into t2
				  from topic in t2.DefaultIfEmpty()
				  join authorInfo in DbContext.AuthorInfo.AsNoTracking().Where(x => x.Status == ConstValues.Status.正常 && x.IsPresenter)
					  on contributeInfo.ID equals authorInfo.ContributeId into t3
				  from author in t3.DefaultIfEmpty()
				  join scheduleInfo in DbContext.ScheduleInfo.AsNoTracking()
					  on contributeInfo.ID equals scheduleInfo.ContributeId into t4
				  from scheduleInf in t4.DefaultIfEmpty()
				  join scheduleMaster in DbContext.ScheduleMaster.AsNoTracking().Where(x => x.Status == ConstValues.Status.正常)
					  on scheduleInf.ScheduleId equals scheduleMaster.ID into t5
				  from scheduleMst in t5.DefaultIfEmpty()
				  select new
				  {
					  Id = contributeInfo.ID,
					  ConferenceId = contributeInfo.ConferenceId,
					  IsInternational = conferenceMaster.IsInternational,
					  ScheduleDate = scheduleInf == null ? new DateTime() : scheduleInf.ScheduleDate,
					  StartTime = scheduleInf == null ? new TimeSpan(0) : scheduleInf.StartTime,
					  EndTime = scheduleInf == null ? new TimeSpan(0) : scheduleInf.EndTime,
					  SchedulePlace = scheduleMst.SchedulePlace,
					  ReportId = contributeInfo.ReportId,
					  ReportType = report.ReportType,
					  TopicId = contributeInfo.TopicId,
					  TopicName = topic.TopicName,
					  EssayTitle = contributeInfo.EssayTitle,
					  PresenterId = author.ID,
					  FullName = author.FullName,
					  Mail = author.Mail,
					  //Affiliation = author.Affiliation,
					  ScheduleStatus = contributeInfo.ScheduleStatus,
					  IsConfirm = contributeInfo.IsConfirm,
					  Remark = contributeInfo.Remark,
					  ScheduleStatusFlag = contributeInfo.ScheduleStatus == ConstValues.ScheduleStatus.已安排 ? 1 :
							  contributeInfo.ScheduleStatus == ConstValues.ScheduleStatus.已取消 ? 2 :
							  contributeInfo.ScheduleStatus == ConstValues.ScheduleStatus.待安排 ? 3 : 4,
					  Attendance = author.Attendance,
					  Description = author.Description,
				  };

			if (!string.IsNullOrEmpty(model.SelReport))
			{
				sql = sql.Where(x => x.ReportId == model.SelReport);
			}

			if (!string.IsNullOrWhiteSpace(model.SelSchedulePlace))
			{
				sql = sql.Where(x => x.SchedulePlace.Contains(model.SelSchedulePlace));
			}

			if (!string.IsNullOrEmpty(model.SelTopic))
			{
				sql = sql.Where(x => x.TopicId == model.SelTopic);
			}

			if (!string.IsNullOrWhiteSpace(model.SelNameAffiliation))
			{
				var contributeAuthorList = DbContext.AuthorInfo.AsNoTracking()
						.Where(x => x.ContributeId.Contains(model.SelNameAffiliation) ||
							x.FullName.Contains(model.SelNameAffiliation) ||
							x.Mail.Contains(model.SelNameAffiliation))
						.Select(x => x.ContributeId)
						.ToList();

				var contributeAffiliationList = DbContext.AffiliationInfo.AsNoTracking()
						.Where(x => x.ContributeId.Contains(model.SelNameAffiliation) ||
							x.Affiliation.Contains(model.SelNameAffiliation))
						.Select(x => x.ContributeId)
						.ToList();

				sql = sql.Where(x => contributeAuthorList.Contains(x.Id) || contributeAffiliationList.Contains(x.Id));
			}

			int total = await sql.CountAsync();

			if (!string.IsNullOrEmpty(sort))
			{
				sql = sql.OrderBy(sort + ConstValues.chrSpace + order).Skip(skip).Take(take);
			}
			else
			{
				sql = sql.OrderBy(x => x.ScheduleStatusFlag).ThenBy(x => x.SchedulePlace).ThenBy(x => x.ScheduleDate).ThenBy(x => x.StartTime).Skip(skip).Take(take);
			}

			var sqlResult = await sql.ToListAsync();

			var result = new List<ConferenceScheduleReportModel>();
			sqlResult.ForEach(y =>
			{
				var affiliationList = DbContext.AffiliationInfo.AsNoTracking()
					.Where(x => x.Status == ConstValues.Status.正常 &&
						x.ConferenceId == y.ConferenceId &&
						x.ContributeId == y.Id &&
						x.Mail == y.Mail)
					.OrderBy(x => x.Sort)
					.ThenBy(x => x.Affiliation)
					.Select(x => x.Affiliation)
					.ToList();
				var strAffiliation = affiliationList.Count > 0 ? string.Join(ConstValues.strSemicolon, affiliationList) : string.Empty;

				var authorList = DbContext.AuthorInfo.AsNoTracking()
					.Where(x => x.ContributeId == y.Id &&
						x.ConferenceId == y.ConferenceId)
					.OrderBy(x => x.Sort)
					.Select(x => x.FullName);

				result.Add(new ConferenceScheduleReportModel()
				{
					Id = y.Id,
					ScheduleDate = y.ScheduleDate,
					StartTime = y.StartTime == new TimeSpan(0) ? string.Empty : y.StartTime.ToString().Substring(0, 5),
					EndTime = y.EndTime == new TimeSpan(0) ? string.Empty : y.EndTime.ToString().Substring(0, 5),
					ScheduleTime = y.StartTime == new TimeSpan(0) ? string.Empty :
						  y.IsInternational == false ?
							  y.ScheduleDate.ToString("yyyy-MM-dd") +
							  " " + y.StartTime.ToString().Substring(0, 5) +
							  ConstValues.strWave + y.EndTime.ToString().Substring(0, 5) :
							  y.ScheduleDate.ToString("MM/dd/yyyy") +
							  " " + y.StartTime.ToString().Substring(0, 5) +
							  ConstValues.strWave + y.EndTime.ToString().Substring(0, 5),
					SchedulePlace = y.SchedulePlace,
					ReportId = y.ReportId,
					ReportType = y.ReportType,
					TopicId = y.TopicId,
					TopicName = y.TopicName,
					EssayTitle = y.EssayTitle,
					FullName = y.FullName,
					Affiliation = strAffiliation,
					PresenterId = DbContext.AuthorInfo.AsNoTracking().FirstOrDefault(x => x.IsPresenter && x.ContributeId == y.Id) == null ?
								string.Empty :
								DbContext.AuthorInfo.AsNoTracking().FirstOrDefault(x => x.IsPresenter && x.ContributeId == y.Id).ID,
					Author = string.Join(ConstValues.strSemicolon, authorList),
					ScheduleStatus = y.ScheduleStatus == ConstValues.ScheduleStatus.已安排 ? Resources.ScheduleStatusDone :
							  y.ScheduleStatus == ConstValues.ScheduleStatus.已取消 ? Resources.ScheduleStatusCancel :
							  y.ScheduleStatus == ConstValues.ScheduleStatus.待安排 ? Resources.ScheduleStatusWait : Resources.ScheduleStatusNo,
					IsConfirm = y.IsConfirm,
					Remark = y.Remark,
					ScheduleStatusFlag = y.ScheduleStatus == ConstValues.ScheduleStatus.已安排 ? 1 :
								y.ScheduleStatus == ConstValues.ScheduleStatus.已取消 ? 2 :
								y.ScheduleStatus == ConstValues.ScheduleStatus.待安排 ? 3 : 4,
					Attendance = y.Attendance,
					Description = y.Description,
				});
			});

			return JsonCamelCase(new
			{
				Total = total,
				Rows = result
			},
			JsonRequestBehavior.AllowGet);
		}

		public async Task<JsonCamelCaseResult> GetEditData(string id)
		{
			var result = await DbContext.ConferenceContributeInfo.AsNoTracking().FirstOrDefaultAsync(x => x.ID == id);

			if (result != null)
			{
				return JsonCamelCase(Utility.CreateSuccessJsonResult(string.Empty, result),
						JsonRequestBehavior.AllowGet);
			}
			else
			{
				return JsonCamelCase(Utility.CreateFailJsonResult(Resources.MsgRecordNotExist),
						JsonRequestBehavior.AllowGet);
			}
		}

		public async Task<JsonCamelCaseResult> GetLogData(string id)
		{
			var result = await DbContext.AuthorInfo.AsNoTracking().FirstOrDefaultAsync(x => x.ContributeId == id && x.IsPresenter);

			if (result != null)
			{
				return JsonCamelCase(Utility.CreateSuccessJsonResult(string.Empty, result),
						JsonRequestBehavior.AllowGet);
			}
			else
			{
				return JsonCamelCase(Utility.CreateFailJsonResult(Resources.MsgRecordNotExist),
						JsonRequestBehavior.AllowGet);
			}
		}

		[HttpPost]
		[ValidateAntiForgeryToken]
		[Route("Modify/{ConferenceId}")]
		public async Task<JsonResult> Modify(ConferenceScheduleReportModel model)
		{
			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName, "Index", model.ConferenceId);

			var result = await DbContext.ConferenceContributeInfo
					.FirstOrDefaultAsync(x => x.ID == model.Id);

			if (result != null)
			{
				result.Remark = model.Remark?.Trim();
				result.Updater = LoginUser.UserName;
				result.UpdateIP = LoginUser.IP;
				result.UpdateDate = DateTime.Now;

				await DbContext.SaveChangesAsync();

				return JsonCamelCase(Utility.CreateSuccessJsonResult(Resources.SuccessSubmit),
						JsonRequestBehavior.AllowGet);
			}
			else
			{
				return JsonCamelCase(Utility.CreateFailJsonResult(Resources.MsgRecordNotExist),
						JsonRequestBehavior.AllowGet);
			}
		}

		[HttpPost]
		[ValidateAntiForgeryToken]
		[Route("ChangeConfirm/{ConferenceId}")]
		public async Task<JsonResult> ChangeConfirm(ConferenceScheduleReportModel model)
		{
			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName, "Index", model.ConferenceId);

			var result = await DbContext.ConferenceContributeInfo
					.FirstOrDefaultAsync(x => x.ID == model.Id);

			if (result != null)
			{
				if (result.IsConfirm)
				{
					result.IsConfirm = false;
					result.ScheduleStatus = ConstValues.ScheduleStatus.无;
				}
				else
				{
					result.IsConfirm = true;
					if (result.ScheduleStatus == ConstValues.ScheduleStatus.无)
					{
						result.ScheduleStatus = ConstValues.ScheduleStatus.待安排;
					}
				}

				result.Updater = LoginUser.UserName;
				result.UpdateIP = LoginUser.IP;
				result.UpdateDate = DateTime.Now;

				await DbContext.SaveChangesAsync();

				return JsonCamelCase(Utility.CreateSuccessJsonResult(Resources.SuccessSubmit),
						JsonRequestBehavior.AllowGet);
			}
			else
			{
				return JsonCamelCase(Utility.CreateFailJsonResult(Resources.MsgRecordNotExist),
						JsonRequestBehavior.AllowGet);
			}
		}

		[HttpPost]
		[ValidateAntiForgeryToken]
		public async Task<FileResult> Export(string allId, string conferenceId)
		{
			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName, "Index", conferenceId);

			var ids = allId.Split(ConstValues.chrComma);
			var sql = from authorInfo in DbContext.AuthorInfo.AsNoTracking()
					.Where(x => ids.Contains(x.ID) &&
						x.ConferenceId == conferenceId &&
						x.Status == ConstValues.Status.正常 &&
						!string.IsNullOrEmpty(x.Attendance))
				  join contributeInfo in DbContext.ConferenceContributeInfo.AsNoTracking()
					.Where(x => !string.IsNullOrEmpty(x.AdminStatus) ? x.AdminStatus == ConstValues.ContributationStatus.录用 :
						x.ContributationStatus == ConstValues.ContributationStatus.录用)
					on authorInfo.ContributeId equals contributeInfo.ID
				  join conferenceMaster in DbContext.ConferenceMaster.AsNoTracking().Where(x => x.Status == ConstValues.Status.正常)
					on contributeInfo.ConferenceId equals conferenceMaster.ID
				  join reportMaster in DbContext.ReportMaster.AsNoTracking().Where(x => x.Status == ConstValues.Status.正常)
					on contributeInfo.ReportId equals reportMaster.ID into t1
				  from report in t1.DefaultIfEmpty()
				  join topicMaster in DbContext.TopicMaster.AsNoTracking().Where(x => x.Status == ConstValues.Status.正常)
					on contributeInfo.TopicId equals topicMaster.ID into t2
				  from topic in t2.DefaultIfEmpty()
				  join scheduleInfo in DbContext.ScheduleInfo.AsNoTracking()
					on contributeInfo.ID equals scheduleInfo.ContributeId
				  join scheduleMaster in DbContext.ScheduleMaster.AsNoTracking().Where(x => x.Status == ConstValues.Status.正常)
					on scheduleInfo.ScheduleId equals scheduleMaster.ID
				  select new
				  {
					  Id = authorInfo.ID,
					  ConferenceId = contributeInfo.ConferenceId,
					  IsInternational = conferenceMaster.IsInternational,
					  ScheduleDate = scheduleInfo.ScheduleDate,
					  StartTime = scheduleInfo.StartTime,
					  EndTime = scheduleInfo.EndTime,
					  SchedulePlace = scheduleMaster.SchedulePlace,
					  ReportType = report.ReportType,
					  TopicName = topic.TopicName,
					  ContributeId = contributeInfo.ID,
					  EssayTitle = contributeInfo.EssayTitle,
					  //Affiliation = authorInfo.Affiliation,
					  IsConfirm = contributeInfo.IsConfirm,
					  ScheduleStatus = contributeInfo.ScheduleStatus,
					  Remark = contributeInfo.Remark,
					  Attendance = authorInfo.Attendance,
					  Description = authorInfo.Description,
					  Summary = authorInfo.Summary,
					  FullName = authorInfo.FullName,
					  Mail = authorInfo.Mail,
				  };

			var sqlResult = await sql.OrderBy(x => x.SchedulePlace).ThenBy(x => x.ScheduleDate).ThenBy(x => x.StartTime).ToListAsync();

			var result = new List<ConferenceScheduleReportModel>();
			sqlResult.ForEach(y =>
			{
				var affiliationList = DbContext.AffiliationInfo.AsNoTracking()
					.Where(x => x.Status == ConstValues.Status.正常 &&
						x.ConferenceId == y.ConferenceId &&
						x.ContributeId == y.Id &&
						x.Mail == y.Mail)
					.OrderBy(x => x.Sort)
					.ThenBy(x => x.Affiliation)
					.Select(x => x.Affiliation)
					.ToList();
				var strAffiliation = affiliationList.Count > 0 ? string.Join(ConstValues.strSemicolon, affiliationList) : string.Empty;

				result.Add(new ConferenceScheduleReportModel()
				{
					Id = y.Id,
					ScheduleTime = y.IsInternational == false ?
							  y.ScheduleDate.ToString("yyyy-MM-dd") +
							  ConstValues.strSpace + y.StartTime.ToString().Substring(0, 5) +
							  ConstValues.strWave + y.EndTime.ToString().Substring(0, 5) :
							  y.ScheduleDate.ToString("MM/dd/yyyy") +
							  ConstValues.strSpace + y.StartTime.ToString().Substring(0, 5) +
							  ConstValues.strWave + y.EndTime.ToString().Substring(0, 5),
					SchedulePlace = y.SchedulePlace,
					ReportType = y.ReportType,
					TopicName = y.TopicName,
					ContributeId = y.ContributeId,
					EssayTitle = y.EssayTitle,
					Affiliation = strAffiliation,
					IsConfirm = y.IsConfirm,
					ScheduleStatus = y.ScheduleStatus,
					Remark = y.Remark,
					Attendance = y.Attendance,
					Description = y.Description,
					Summary = y.Summary,
					FullName = y.FullName
				});
			});

			var export = Request.Cookies["_culture"].Value == ConstValues.Language.Cn ?
					Resources.export_presentationLogCN : Resources.export_presentationLogEN;

			XSSFWorkbook workbook;

			using (MemoryStream readStream = new MemoryStream(export))
			{
				workbook = new XSSFWorkbook(readStream);
				ISheet sheet = workbook.GetSheetAt(0);

				var count = 0;
				foreach (var model in result)
				{
					count += 1;
					IRow row = sheet.CreateRow(count);

					// 报告时间
					int column = 0;
					ICell cell = row.CreateCell(column);
					cell.SetCellValue(model.ScheduleTime);

					// 日程会场名称
					column++;
					cell = row.CreateCell(column);
					cell.SetCellValue(model.SchedulePlace);

					// 报告类型
					column++;
					cell = row.CreateCell(column);
					cell.SetCellValue(model.ReportType);

					// 专题名称
					column++;
					cell = row.CreateCell(column);
					cell.SetCellValue(model.TopicName);

					// 稿件编号
					column++;
					cell = row.CreateCell(column);
					cell.SetCellValue(model.ContributeId);

					// 论文题目
					column++;
					cell = row.CreateCell(column);
					cell.SetCellValue(model.EssayTitle);

					// 报告人
					column++;
					cell = row.CreateCell(column);
					cell.SetCellValue(model.FullName);

					// 单位
					column++;
					cell = row.CreateCell(column);
					cell.SetCellValue(model.Affiliation);

					// 出席确认
					column++;
					cell = row.CreateCell(column);
					cell.SetCellValue(model.IsConfirm ? Resources.Yes : Resources.No);

					// 日程状态
					column++;
					cell = row.CreateCell(column);
					cell.SetCellValue(model.ScheduleStatus == ConstValues.ScheduleStatus.已安排 ? Resources.ScheduleStatusDone :
							model.ScheduleStatus == ConstValues.ScheduleStatus.已取消 ? Resources.ScheduleStatusCancel :
							model.ScheduleStatus == ConstValues.ScheduleStatus.待安排 ? Resources.ScheduleStatusWait :
							Resources.ScheduleStatusNo);

					// 备注
					column++;
					cell = row.CreateCell(column);
					cell.SetCellValue(model.Remark);

					// 会场出席情况
					column++;
					cell = row.CreateCell(column);
					cell.SetCellValue(model.Attendance);

					// 出席情况说明
					column++;
					cell = row.CreateCell(column);
					cell.SetCellValue(model.Description);

					// 会场记录
					column++;
					cell = row.CreateCell(column);
					cell.SetCellValue(model.Summary);
				}
			}

			MemoryStream exportStream = new MemoryStream();
			workbook.Write(exportStream);
			exportStream.Flush();
			exportStream.Position = 0;

			return File(exportStream, Application.Octet,
					$"export_presentationLog_{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx");
		}

		public async Task<JsonCamelCaseResult> SetAllConfirm(ConferenceScheduleReportModel model)
		{
			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName, "Index", model.ConferenceId);

			var ids = model.HdnAllConfirmIds.Split(ConstValues.chrComma);
			var sql = DbContext.ConferenceContributeInfo.Where(x => ids.Contains(x.ID.ToString())).ToList();

			if (sql.Count > 0)
			{
				if (model.AllConfirmFlag == "true")
				{
					using (var transaction = DbContext.Database.BeginTransaction())
					{
						foreach (var data in sql)
						{
							data.IsConfirm = true;
							if (data.ScheduleStatus == ConstValues.ScheduleStatus.无)
							{
								data.ScheduleStatus = ConstValues.ScheduleStatus.待安排;
							}
						}
						await DbContext.SaveChangesAsync();
						transaction.Commit();
					}

					return JsonCamelCase(Utility.CreateSuccessJsonResult(string.Empty),
							JsonRequestBehavior.AllowGet);
				}
				else if (model.AllConfirmFlag == "false")
				{
					using (var transaction = DbContext.Database.BeginTransaction())
					{
						foreach (var data in sql)
						{
							if (data.ScheduleStatus == ConstValues.ScheduleStatus.待安排)
							{
								data.IsConfirm = false;
								data.ScheduleStatus = ConstValues.ScheduleStatus.无;
							}
						}
						await DbContext.SaveChangesAsync();
						transaction.Commit();
					}
					await DbContext.SaveChangesAsync();
					return JsonCamelCase(Utility.CreateSuccessJsonResult(string.Empty),
							JsonRequestBehavior.AllowGet);
				}
				else
				{
					return JsonCamelCase(Utility.CreateSuccessJsonResult(string.Empty),
							JsonRequestBehavior.AllowGet);
				}

			}
			else
			{
				return JsonCamelCase(Utility.CreateFailJsonResult(Resources.MsgRecordNotExist),
						JsonRequestBehavior.AllowGet);
			}
		}

		[HttpPost]
		[ValidateAntiForgeryToken]
		[Route("SaveLogData/{ConferenceId}")]
		public async Task<JsonResult> SaveLogData(ConferenceScheduleReportModel model)
		{
			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName, "Index", model.ConferenceId);

			var result = await DbContext.AuthorInfo
					.FirstOrDefaultAsync(x => x.ContributeId == model.LogId && x.IsPresenter);

			if (result != null)
			{
				result.Attendance = model.Attendance?.Trim();
				result.Description = model.Description?.Trim();
				result.Summary = model.Summary?.Trim();
				result.Updater = LoginUser.UserName;
				result.UpdateIP = LoginUser.IP;
				result.UpdateDate = DateTime.Now;

				await DbContext.SaveChangesAsync();

				return JsonCamelCase(Utility.CreateSuccessJsonResult(Resources.SuccessSubmit),
						JsonRequestBehavior.AllowGet);
			}
			else
			{
				return JsonCamelCase(Utility.CreateFailJsonResult(Resources.MsgRecordNotExist),
						JsonRequestBehavior.AllowGet);
			}
		}
	}
}